package com.lxy.cxcyxmmsbackend.sys.service.impl;

import com.lxy.cxcyxmmsbackend.sys.entity.QxglCd;
import com.lxy.cxcyxmmsbackend.sys.entity.QxglDz;
import com.lxy.cxcyxmmsbackend.sys.exceptionHandle.exce.NormalException;
import com.lxy.cxcyxmmsbackend.sys.mapper.CommonMapper;
import com.lxy.cxcyxmmsbackend.sys.mapper.QxglCdMapper;
import com.lxy.cxcyxmmsbackend.sys.mapper.QxglDzMapper;
import com.lxy.cxcyxmmsbackend.sys.res.Result;
import com.lxy.cxcyxmmsbackend.sys.service.QxglCdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘新颖
 * @since 2024-01-07
 */
@Service
public class QxglCdServiceImpl extends ServiceImpl<QxglCdMapper, QxglCd> implements QxglCdService {

    @Resource
    QxglCdMapper qxglCdMapper;
    @Resource
    QxglDzMapper qxglDzMapper;
    @Resource
    CommonMapper commonMapper;

    @Override
    @Transactional
    public Result selectQxglCdByCdMc(String menu_name) {
        List<QxglCd> qxglCdList = qxglCdMapper.selectQxglCdByCdMc(menu_name);
        if (qxglCdList == null || qxglCdList.size() == 0) {
            throw new NormalException("查询菜单失败");
        }
        return Result.success("查询菜单成功", qxglCdList);
    }

    @Override
    @Transactional
    public Result selectQxglCds() {
        List<QxglCd> qxglCdList = qxglCdMapper.selectQxglCds();
        if (qxglCdList == null || qxglCdList.size() == 0) {
            throw new NormalException("查询菜单失败");
        }
        return Result.success("查询菜单成功", qxglCdList);
    }

    @Override
    @Transactional
    public Result insertQxglCd(QxglCd qxglCd) {
        if (Integer.parseInt(qxglCd.getCdDj()) >= 3) {
            throw new NormalException("暂不可添加三级菜单");
        }
        String cdBh = commonMapper.getNewBhMenu();
        qxglCd.setCdBh(cdBh);
        qxglCd.setCdCjsj(new Date(System.currentTimeMillis()));
        int i = qxglCdMapper.insertQxglCd(qxglCd);
        if (i != 1) {
            throw new NormalException("新增菜单失败");
        }
        return Result.success("新增菜单成功", qxglCd);
    }

    @Override
    @Transactional
    public Result updateQxglCd(QxglCd qxglCd) {
        qxglCd.setCdGxsj(new Date(System.currentTimeMillis()));
        int i = qxglCdMapper.updateQxglCd(qxglCd);
        if (i != 1) {
            throw new NormalException("修改菜单失败");
        }
        return Result.success("修改菜单成功", qxglCd);
    }

    @Override
    @Transactional
    public Result deleteQxglCd(String CD_BH) {
        int i = qxglCdMapper.deleteQxglCd(CD_BH);
        if (i != 1) {
            throw new NormalException("删除菜单失败");
        }
        //删除菜单角色关系
        int i1 = qxglCdMapper.deleteRoleMenuRel(CD_BH);
        int i2 = qxglDzMapper.deleteQxglDzByMId(CD_BH);
        //表单校验

        HashMap<String, Object> map = new HashMap<>();
        map.put("deleteQxglCd", i);
        map.put("deleteQxglDzByMId", i2);
        map.put("deleteRoleMenuRel", i1);
        return Result.success("删除菜单成功", map);
    }
}
